<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\OrdersExporter;
use App\Http\Middleware\TrimStrings;
use App\Models\Order;
use App\Models\OrderItem;
use App\Models\Product;
use App\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;

class OrdersController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '订单';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Order);
        $grid->column('id', __('Id'))->sortable();
        $grid->column('no', __('订单号'))->expand(function($model){
            $item = $model->OrderItems()->get()->map(function ($item) {
                $orderItem =  $item->only(['product_id','sku_name', 'amount', 'price']);
                $product = Product::find($orderItem['product_id']);
                $title = $product ? $product['title']:'';
                $orderItem = array_merge(['title'=>$title],$orderItem);
                unset($orderItem['product_id']);
                return $orderItem;
            });
            return new Table(['商品','购买规格','数量','价格'], $item->toArray());
        });
        $grid->column('user_id', __('下单用户'))->display(function($user_id){
            $user = User::find($user_id);
            if ($user) {
                $user = $user['mobile'];
            }
            return $user;
        });
        $grid->column('total_amount', __('总价'));
        $grid->column('express_fee', __('快递费'));
        $grid->column('order_status', __('订单状态'))->editable('select',Order::ORDER_STATUS_DESC);
        $grid->column('is_closed', __('取消'))->editable('select',Order::IS_CLOSED);
        $grid->column('created_at', __('提交时间'))->sortable();
        $grid->column('paid_at', __('支付时间'))->sortable();
        $grid->column('sended_at', __('发货时间'))->sortable();
        $grid->column('contact_name', __('收货人'));
        $grid->column('contact_phone', __('收货电话'));
        $grid->actions(function ($actions) {
            //$actions->disableDelete();
            $actions->disableView();
        });
        $grid->model()->orderBy('id','desc');
        //$grid->model()->where('is_pay','=','1');
        $grid->filter(function($filter){
            $filter->disableIdFilter();
            $filter->equal('no', '订单号');
            $filter->equal('contact_name', '收货人');
            $filter->equal('contact_phone', '收货电话');
            $filter->between('created_at', '提交时间')->datetime();
            $filter->between('paid_at', '支付时间')->datetime();
            $status = Order::ORDER_STATUS_DESC;
            unset($status[0]);
            $filter->equal('order_status', '状态')->radio($status);

        });
        $grid->disableCreateButton();
        $grid->exporter(new OrdersExporter());
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Order::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('no', __('No'));
        $show->field('user_id', __('User id'));
        $show->field('address', __('Address'));
        $show->field('total_amount', __('Total amount'));
        $show->field('buy_type', __('Buy type'));
        $show->field('score_amount', __('Score amount'));
        $show->field('real_pay_money', __('Real pay money'));
        $show->field('remark', __('Remark'));
        $show->field('paid_at', __('Paid at'));
        $show->field('payment_method', __('Payment method'));
        $show->field('is_pay', __('Is pay'));
        $show->field('is_send', __('Is send'));
        $show->field('is_receive', __('Is receive'));
        $show->field('payment_no', __('Payment no'));
        $show->field('is_closed', __('Is closed'));
        $show->field('reviewed', __('Reviewed'));
        $show->field('ship_name', __('Ship name'));
        $show->field('ship_no', __('Ship no'));
        $show->field('sended_at', __('Sended at'));
        $show->field('dealt_at', __('Dealt at'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Order);

        $form->text('no', __('订单号'))->readonly();
        $form->text('contact_name', __('收货人'));
        $form->text('contact_phone', __('收货电话'));
        $form->text('address_detail', __('详细地址'));
        $form->text('remark', __('客户备注'));
        $form->select('order_status', '收货')->options(Order::ORDER_STATUS_DESC);
        //保存后回调
        $form->saved(function (Form $form) {
            $id = $form->model()->id;
            $order = Order::find($id);
            if ($order) {
                if ($form->order_status == 1) {
                    $update = [
                        'is_pay' => 1,
                        'paid_at' => date('Y-m-d H:i:s'),
                    ];
                    $order->update($update);
                }
                if ($form->order_status == 2) {
                    $update = [
                        'is_send' => 1,
                        'sended_at' => date('Y-m-d H:i:s'),
                    ];
                    $order->update($update);
                }
                if ($form->order_status == 3) {
                    $update = [
                        'is_receive' => 1,
                        'receive_at' => date('Y-m-d H:i:s'),
                    ];
                    $order->update($update);
                }

            }

        });
        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
            $tools->disableView();
        });
        $form->footer(function ($footer) {
            $footer->disableViewCheck();
            $footer->disableEditingCheck();
            $footer->disableCreatingCheck();
        });
        return $form;
    }
}
